﻿using hvppleDotNet;
using HZH_Controls.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HangzhouChipProject
{
    public partial class findModel : FrmWithOKCancel1
    {
        HTuple hsmartwindows;
        private Dictionary<string, string> modelUidata = new Dictionary<string, string>(); //传递模板参数
        public static  HTuple row, coumn, angle, score;
        MainWindows mainwindow;
        public findModel(MainWindows mainwindow)
        {
            this.mainwindow = mainwindow;
            this.hsmartwindows = mainwindow.hSmartWindowControl1.hvppleWindow;
            InitializeComponent();
            LoadUiDataToSetting();
            DataSynchronism();
            this.Title = "查找模板";
            this.IsShowCloseBtn = true;
            this.BackColor = Color.FromArgb(128, 128, 255);
            
        }
     
        protected override void DoEnter()
        {
           
            if (SaveUiDataToSetting())
            {
                FrmTips.ShowTipsSuccess(this, "参数保存成功");
            };
        }
        
        protected override void DoEsc()
        {
            base.DoEsc();   
        }
       /// <summary>
       /// 用以传递参数寻找模板
       /// </summary>
        private void getUiData()
        {
            try
            {
                modelUidata["AngleStart"] = this.AngleStart_numericUpDown.Value.ToString();
                modelUidata["AngleExtent"] = this.AngleExtent_numericUpDown.Value.ToString();
                modelUidata["MinScore"] = this.MinScore_numericUpDown.Value.ToString();
                modelUidata["NumMatches"] = this.NumMatches_numericUpDown.Value.ToString();
                modelUidata["MaxOverlap"] = this.MaxOverlap_numericUpDown.Value.ToString();
                modelUidata["NumLevels"] = this.NumLevels_numericUpDown.Value.ToString();
                modelUidata["Greediness"] = this.Greediness_numericUpDown.Value.ToString();         
                modelUidata["SubPixel"] = this.SubPixel_comboBox.Text.ToString();
            }
            catch (Exception ex)
            {

                FrmTips.ShowTipsError(this, ex.ToString());
            }


        }
        private bool SaveUiDataToSetting()
        {
            try
            {
                Properties.FindShapeModelData.Default.AngleStart = this.AngleStart_numericUpDown.Value.ToString();
                Properties.FindShapeModelData.Default.AngleExtent = this.AngleExtent_numericUpDown.Value.ToString();
                Properties.FindShapeModelData.Default.MinScore = this.MinScore_numericUpDown.Value.ToString();
                Properties.FindShapeModelData.Default.NumMatches = this.NumMatches_numericUpDown.Value.ToString();
                Properties.FindShapeModelData.Default.MaxOverlap = this.MaxOverlap_numericUpDown.Value.ToString();
                Properties.FindShapeModelData.Default.NumLevels = this.NumLevels_numericUpDown.Value.ToString();
                Properties.FindShapeModelData.Default.Greediness = this.Greediness_numericUpDown.Value.ToString();
                Properties.FindShapeModelData.Default.SubPixel = this.SubPixel_comboBox .Text.ToString();
                Properties.FindShapeModelData.Default.Save();
                return true;
            }
            catch (Exception)
            {

                return false;
            }

        }
        private void DataSynchronism()
        {
            this.AngleStart_ucTrackBar.Value = (float)this.AngleStart_numericUpDown.Value;
            this.AngleExtent_ucTrackBar.Value = (float)this.AngleExtent_numericUpDown.Value;
            this.MinScore_ucTrackBar.Value = (float)this.MinScore_numericUpDown.Value;
            this.NumMatches_ucTrackBar.Value = (float)this.NumMatches_numericUpDown.Value;
            this.MaxOverlap_ucTrackBar.Value = (float)this.MaxOverlap_numericUpDown.Value;
            this.NumLevels_ucTrackBar.Value = (float)this.NumLevels_numericUpDown.Value;
            this.Greediness_ucTrackBar.Value = (float)this.Greediness_numericUpDown.Value;
            this.Greediness_ucTrackBar.Value = (float)this.Greediness_numericUpDown.Value;
        }
        /// <summary>
        /// 将保存的数据载入到界面使用
        /// </summary>
        private void LoadUiDataToSetting()
        {
            try
            {
                this.AngleStart_numericUpDown.Value = Convert.ToDecimal(Properties.FindShapeModelData.Default.AngleStart);
                this.AngleExtent_numericUpDown.Value = Convert.ToDecimal(Properties.FindShapeModelData.Default.AngleExtent);
                this.MinScore_numericUpDown.Value = Convert.ToDecimal(Properties.FindShapeModelData.Default.MinScore);
                this.NumMatches_numericUpDown.Value = Convert.ToDecimal(Properties.FindShapeModelData.Default.NumMatches);
                this.MaxOverlap_numericUpDown.Value = Convert.ToDecimal(Properties.FindShapeModelData.Default.MaxOverlap);
                this.NumLevels_numericUpDown.Value = Convert.ToDecimal(Properties.FindShapeModelData.Default.NumLevels);
                this.Greediness_numericUpDown.Value = Convert.ToDecimal(Properties.FindShapeModelData.Default.Greediness);
                this.SubPixel_comboBox.Text = Properties.FindShapeModelData.Default.SubPixel;

            }
            catch (Exception ex)
            {

                FrmTips.ShowTipsError(this, "载入查找模板参数数据时："+ex.ToString());
            }

        }
        protected override void OnLoad(EventArgs e)
        {
            SubPixel_comboBox.SelectedIndex = 0;
            AngleStart_ucTrackBar.ValueChanged += (s, se) => { this.AngleStart_numericUpDown.Value = Convert.ToDecimal(this.AngleStart_ucTrackBar.Value); };
            AngleExtent_ucTrackBar.ValueChanged += (s, se) => { this.AngleExtent_numericUpDown.Value = Convert.ToDecimal(this.AngleExtent_ucTrackBar.Value); };
            MinScore_ucTrackBar.ValueChanged += (s, se) => { this.MinScore_numericUpDown.Value = Convert.ToDecimal(this.MinScore_ucTrackBar.Value); };
            NumMatches_ucTrackBar.ValueChanged += (s, se) => { this.NumMatches_numericUpDown.Value = Convert.ToDecimal(this.NumMatches_ucTrackBar.Value); };
            MaxOverlap_ucTrackBar.ValueChanged += (s, se) => { this.MaxOverlap_numericUpDown.Value = Convert.ToDecimal(this.MaxOverlap_ucTrackBar.Value); };
            NumLevels_ucTrackBar.ValueChanged += (s, se) => { this.NumLevels_numericUpDown.Value = Convert.ToDecimal(this.NumLevels_ucTrackBar.Value); };
            Greediness_ucTrackBar.ValueChanged += (s, se) => { this.Greediness_numericUpDown.Value = Convert.ToDecimal(this.Greediness_ucTrackBar.Value); };
        }

        private void AngleStart_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            this.AngleStart_ucTrackBar.Value =(float) this.AngleStart_numericUpDown.Value;
        }

        private void AngleExtent_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
              this.AngleExtent_ucTrackBar.Value= (float)this.AngleExtent_numericUpDown.Value;
        }

        private void MinScore_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
             this.MinScore_ucTrackBar.Value= (float)this.MinScore_numericUpDown.Value;
        }
        
        private void NumMatches_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
             this.NumMatches_ucTrackBar.Value=(float)this.NumMatches_numericUpDown.Value;
        }

        private void MaxOverlap_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            this.MaxOverlap_ucTrackBar.Value = (float)this.MaxOverlap_numericUpDown.Value;
        }

        private void NumLevels_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            this.NumLevels_ucTrackBar.Value= (float)this.NumLevels_numericUpDown.Value;
        }

        private void Greediness_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
             this.Greediness_ucTrackBar.Value=(float) this.Greediness_numericUpDown.Value;
        }

        private void ucBtnExt_StartFind_BtnClick(object sender, EventArgs e)
        {
            if (cReataModel.modelID.Length >0 && InterLayer.hImage!=null )
            {
                HObject ContoursCrossEmtyobj;
                getUiData();
                HObject hfindcontours= PictureProcess.FindModel(InterLayer.hImage, cReataModel.modelID, modelUidata, out ContoursCrossEmtyobj,  out  row, out coumn, out angle, out score);              
                HOperatorSet.SetDraw(hsmartwindows, "margin");
                HOperatorSet.DispObj(hfindcontours, hsmartwindows);
                HOperatorSet.DispObj(ContoursCrossEmtyobj, hsmartwindows);
                this.mainwindow.PrintfLog(string.Format("一共找到{0}个产品",score.Length));
            }
                
        }
    }
}
